package com.ruoyu.cloud.ai.mcp.controller;

import com.ruoyu.cloud.ai.mcp.respository.ChatHistoryRespository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

@RequiredArgsConstructor
@CrossOrigin
@RestController
@RequestMapping("/ai/")
public class ChatController {

        private final ChatClient chatClient;
//
    private final ChatHistoryRespository chatHistoryRespository;



    @RequestMapping(value = "chat", produces = "text/html;charset=utf-8")
    public String chat(String question, String chatId) {
        //1.保存会话id
        chatHistoryRespository.save("chat", chatId);
        //2.请求模型
        return chatClient.prompt()
                .user(question)
                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .call()
                .content();

    }

}
